using Ebusiness_Framework.ConfigCenter;
using Ebusiness_Framework.ConfigLoad;
using Ebusiness_Framework.LogCenter;
using ProductService.Data;
using Serilog;
using Serilog.Events;
using Volo.Abp.Data;

namespace ProductService;

public class Program
{
    public async static Task<int> Main(string[] args)
    {
        //if (IsMigrateDatabase(args))
        //{
        //    loggerConfiguration.MinimumLevel.Override("Volo.Abp", LogEventLevel.Warning);
        //    loggerConfiguration.MinimumLevel.Override("Microsoft", LogEventLevel.Warning);
        //}
        try
        {
            var builder = WebApplication.CreateBuilder(args);
            builder.Host.AddAppSettingsSecretsJson()
                .UseAutofac();

            //启动配置
            RunConfiguration(builder);

            //if (IsMigrateDatabase(args))
            //{
            //    builder.Services.AddDataMigrationEnvironment();
            //}
            await builder.AddApplicationAsync<ProductServiceModule>();
            var app = builder.Build();
            await app.InitializeApplicationAsync();

            //if (IsMigrateDatabase(args))
            //{
            //    return 0;
            //}

            Log.Information("Starting ProductService.");
            await app.RunAsync();
            return 0;
        }
        catch (Exception ex)
        {
            if (ex is HostAbortedException)
            {
                throw;
            }

            Log.Fatal(ex, "ProductService terminated unexpectedly!");
            return 1;
        }
        finally
        {
            Log.CloseAndFlush();
        }
    }

    /// <summary>
    /// 启动配置
    /// </summary>
    private static void RunConfiguration(WebApplicationBuilder builder)
    {
        #region 配置 加载 appsettings.json 文件
        {
            //builder.UseConfigLoad();
        }
        #endregion

        #region 集成NLog  日志中心 ELK
        {
            builder.AddDistributedLog();
        }
        #endregion

        #region 加载nacos配置  配置中心 nacos //加载nacos配置 
        {
            //builder.UseConfigCenter();  //加载之后就能在配置中心获取数据了
        }
        #endregion
    }

    private static bool IsMigrateDatabase(string[] args)
    {
        return args.Any(x => x.Contains("--migrate-database", StringComparison.OrdinalIgnoreCase));
    }
}
